﻿using System;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MetMax.Importer
{
    class RetentionAnalysis : Base
    {
        #region constructors
        public RetentionAnalysis(MetMaxDataSet.resultDataTable dt, string[] filenames, Options opt)
            : base(dt != null ? dt : new MetMaxDataSet.resultDataTable (), filenames, opt)
        {
        }

        public RetentionAnalysis(BackgroundWorkerArgument bgwa, Options opt)
            : this(bgwa.dt as MetMaxDataSet.resultDataTable, bgwa.filenames, opt)
        {
        }
        #endregion constructors

        public new MetMaxDataSet.resultDataTable dt
        {
            get { return base.dt as MetMaxDataSet.resultDataTable; }
        }

        public override void Import(string fullFileName, DataColumn currentfileColumn)
        {
            //load the stuff from the file into the memory
            IEnumerable<BusinessObject.Base> resultListCopy = LoadFile(fullFileName).ToList().AsParallel();

            //compute a fitting for the retention time
            IEnumerable<BusinessObject.Base> retentionstandards = (from p in resultListCopy
                                                              where p.RiMarkerRegExMatch.Success
                                                              select p).AsParallel();
            Fitting.Polynomial poly3fit_1stDimension = getPolynomialFittingDegree3Rt1ToAlkaneIndex(retentionstandards);

            Fitting.Spline RelativeAlkanRetentionTime = null;
            try
            {
                RelativeAlkanRetentionTime = getRelativeAlkanRetentionTime(retentionstandards);
            }
            catch (Exception)
            {
                //MessageBox.Show(exc.Message);
            }

            Fitting.ObservationPointDictionary ri1system = getRi1System(retentionstandards);
            Fitting.ObservationPointDictionary ri2system = getRi2System(retentionstandards);

            //first, run through all observations an build the matrix as well as the prediction systems
            foreach (BusinessObject.Base bo in (from p in resultListCopy /*where p.mass.HasValue*/ select p))
            {
                if (CancellationPending)
                {
                    break;
                }
                //DataRow tmpRow = this.Rows.Find(new object[] { v.GetName, v.mass.Value });
                MetMaxDataSet.resultRow tmpRow = dt.FindBynamemass(bo.GetName, bo.mass);
                if (tmpRow == null)
                {
                    tmpRow = dt.AddresultDataTableRow(bo.GetName, bo.mass, bo.ri.Value);
                }

                tmpRow[currentfileColumn] = new BusinessObject.RetentionAnalysis(
                    bo
                    , poly3fit_1stDimension
                    , RelativeAlkanRetentionTime
                    , ri1system
                    , ri2system);
            }
        }
    }
}
